//
//  SecondViewController3.m
//  UIhomework_14
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "SecondViewController3.h"
#import "MyCollectionViewCell.h"
#import "MovieModel.h"

@interface SecondViewController3 ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *modelArr;
@end

@implementation SecondViewController3


-(void)dealloc{
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   //  Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    self.navigationItem.title = @"电影";
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item;
    
    [self create];
    
    [self dataHandle];
    

    
    
}

//创建collection
- (void)create{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 60) / 3, 200);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    collection.backgroundColor = [UIColor clearColor];
    [self.view addSubview:collection];
    [collection release];
    [layout release];
    collection.delegate = self;
    collection.dataSource = self;
    
    [collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (void)dataHandle{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    self.arr = [NSMutableArray arrayWithContentsOfFile:path];
    self.modelArr = [NSMutableArray array];
    for (NSDictionary *dic in _arr) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
       
    }
     NSLog(@"%@", _modelArr);
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    MovieModel *model = _modelArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _modelArr.count;
}

- (void)itemAction:(UIBarButtonItem *)item{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
